package com.ruoyi.web.service.warning.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.constant.StateConstants;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.web.domain.warning.WarningAddressBook;
import com.ruoyi.web.domain.warning.WarningNotice;
import com.ruoyi.web.mapper.warning.WarningNoticeMapper;
import com.ruoyi.web.pojo.request.warning.WarningNoticeRequest;
import com.ruoyi.web.service.warning.WarningNoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

/**
 * 预警通知配置 服务实现类
 * @author fwx
 * @date 2024-01-31 14:08:37
 */
@Service
public class WarningNoticeServiceImpl extends ServiceImpl<WarningNoticeMapper, WarningNotice> implements WarningNoticeService {

    @Autowired
    private TokenService tokenService;


    /**
    * 新增预警通知配置
    */
    @Override
    public void insert(WarningNotice notice){
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());

        if(loginUser == null){
            throw new RuntimeException("用户未登录");
        }

        notice.setUserId(loginUser.getUserId());
        notice.setUsername(loginUser.getUsername());

        notice.setIsEnable(StateConstants.ENABLE);
        notice.setCrtTime(LocalDateTime.now());

        super.save(notice);
    }

    /**
    * 修改预警通知配置
    */
    @Override
    public void update(WarningNotice WarningNotice){
        super.updateById(WarningNotice);
    }

    @Override
    public void addOrUpdateNotice(WarningNotice notice){
        if(StringUtils.isBlank(notice.getBookId()) || StringUtils.isBlank(notice.getWarningTitle())){
            throw  new RuntimeException("需要配置通知人或者标题");
        }

        if(notice.getId() != null){
            this.update(notice);
            return;
        }

        this.insert(notice);
    }

    /**
    * 删除预警通知配置
    */
    @Override
    public void delete(String ids){
        if(StringUtils.isBlank(ids)){
            throw new RuntimeException("通知配置id不能为空");
        }
        List<String> idList = Arrays.asList(ids.split(","));
        super.removeBatchByIds(idList);
    }

    /**
    *  分页查询预警通知配置
    */
    @Override
    public void queryPage(IPage<WarningNotice> page){
        QueryWrapper<WarningNotice> query  = new QueryWrapper<>();
        super.page(page,query);
    }

    @Override
    public List<WarningNotice> queryPage(WarningNoticeRequest request){
        QueryWrapper<WarningNotice> query  = new QueryWrapper<>();
        query.lambda().orderByDesc(WarningNotice::getCrtTime);
        List<WarningNotice> list = super.list(query);
        return list;
    }

    /**
    * 查询所有预警通知配置
    */
    @Override
    public List<WarningNotice> queryAll(){
        QueryWrapper<WarningNotice> query  = new QueryWrapper<>();
        List<WarningNotice> list = super.list(query);
        return list;
    }

    /**
    * 根据id查询预警通知配置
    */
    @Override
    public WarningNotice findById(String id){
        WarningNotice res = super.getById(id);
        return res;
    }

}
